package com.aisile.common.yongyou;

import com.aisile.common.exception.CustomExceptionThrows;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.alibaba.fastjson.JSON;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 包含 证件ocr识别  身份证二要素检测  活体检测  人证核验检测
 *
 * @Auther:yry
 * @Date:2024/7/17 0017
 * @VERSON:1.0
 */
@Setter
@Component
@PropertySource("classpath:yongyou.properties")
@ConfigurationProperties(prefix = "yongyou")
public class YongYouCommonScan {

    private String matchidentitycode; // 二要素
    private String idcardocrcode; // ocr
    private String biologicalexaminationcode; // 活体检测
    private String personandcertificateverificationcode; // 人证核验

    @Autowired
    private RestTemplate restTemplate;


    public Map idCardOCRSacn(String images) {
        if (StringUtils.isEmpty(images)) {
            return null;
        }
        String url = "https://api.yonyoucloud.com/apis/dst/IdcardOCR/IdcardOCR";
        Map<String, Object> params = new HashMap<String, Object>();//请求参数
        params.put("image", images);
        params.put("ocrType", "0");
        params.put("imageType", "URL");

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode", idcardocrcode);//APICODE
        httpHeaders.add("Content-Type", "application/json");

        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);
        String str = "{\"message\":\"成功\",\"data\":{\"tradeNo\":\"1262796238463332352\",\"code\":\"0\",\"riskType\":\"normal\",\"address\":\"沈阳市东陵区文化东路24-8号1-3-6\",\"birth\":\"19510322\",\"name\":\"王东镇\",\"cardNum\":\"210103195103222113\",\"sex\":\"男\",\"nation\":\"汉\",\"issuingDate\":\"\",\"issuingAuthority\":\"\",\"expiryDate\":\"\"},\"code\":\"601200000\"}";
        //ResponseEntity<Map> responseEntity =  restTemplate.postForEntity(url,httpEntity,Map.class);
        return JSON.parseObject(str, Map.class);
    }


    public boolean matchIdentityScan(String userName, String idNumber, Integer type) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(idNumber)) {
            return false;
        }
        String url = "https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity";//请求接口地址
        Map<String, Object> params = new HashMap<String, Object>();//请求参数
        params.put("idNumber", idNumber);
        params.put("userName", userName);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode", matchidentitycode);//APICODE
        httpHeaders.add("Content-Type", "application/json");


        String success = "{\"success\":true,\"code\":400100,\"message\":\"一致\",\"data\":{\"orderNumber\":\"011721115280137092\"}}";
        String fail = "{\"success\":false,\"code\":400432,\"message\":\"身份证号码不对\",\"data\":null}";


        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);
        Map<String, Object> resultMap = null;
        if (type == null) {
            ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, httpEntity, Map.class);
            resultMap = responseEntity.getBody();
        } else if (type == 1) {
            resultMap = JSON.parseObject(success, Map.class);
        } else if (type == 2) {
            resultMap = JSON.parseObject(fail, Map.class);
        }

        Boolean successes =  (Boolean)resultMap.get("success");
        Integer code = (Integer) resultMap.get("code");
        if(successes && code == 400100){
            return true;
        }
        return false;
    }

    // 正数=疑似  0=成功  负数=失败

    /**
     *
     * @param images
     * @param type 1 true  2 false 3 ask
     * @return
     */
    public int BiologicalexaminationScan(String images,Integer type){
        // 默认都成功 成功啥也不干  疑似++ 失败--
        int i = 0;
        String url ="https://api.yonyoucloud.com/apis/dst/Biologicalexamination/Biologicalexamination";//请求接口地址
        Map<String, Object> params = new HashMap<String, Object>();//请求参数
        params.put("image", "https://m.360buyimg.com/babel/jfs/t1/228791/8/22913/222072/6686021dF418bf267/af3fc259fc0178cd.jpg.webp");
        params.put("imageType", "URL");

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode", biologicalexaminationcode);//APICODE
        httpHeaders.add("Content-Type", "application/json");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSON.toJSONString(params), httpHeaders);


        String success = "{\"message\":\"成功\",\"data\":{\"checkStatus\":\"0\",\"score\":\"95\",\"tradeNo\":\"1262797256843862016\",\"remark\":\"检测成功\",\"code\":\"0\"},\"code\":\"601200000\"}\n";
        String ask = "{\"message\":\"成功\",\"data\":{\"checkStatus\":\"0\",\"score\":\"84\",\"tradeNo\":\"1262797256843862016\",\"remark\":\"检测成功\",\"code\":\"0\"},\"code\":\"601200000\"}\n";
        String fail = "{\"message\":\"成功\",\"data\":{\"checkStatus\":\"0\",\"score\":\"60\",\"tradeNo\":\"1262797256843862016\",\"remark\":\"检测成功\",\"code\":\"0\"},\"code\":\"601200000\"}\n";

        Map<String, Object> resultMap = null;
        if (type == null) {
            ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, httpEntity, Map.class);
            resultMap = responseEntity.getBody();
        } else if (type == 1) {
            resultMap = JSON.parseObject(success, Map.class);
        } else if (type == 2) {
            resultMap = JSON.parseObject(fail, Map.class);
        } else if(type == 3){
            resultMap = JSON.parseObject(ask, Map.class);
        }
        Integer code = Integer.valueOf((String)resultMap.get("code"));
        if(code.equals(601200000)){
            // 开始判断分值
            // score >=90 活人  score<=89 >=70 疑似  score<=69 失败
            Map data = (Map) resultMap.get("data");
            Integer score =  Integer.valueOf((String) data.get("score"));
            if(score <=69 && score >=0){
                i--;
            }else if(score <=89 && score >=70){
                i++;
            }
        }else{
            i--;
        }
        return i;
    }

    /**
     *
     * @param type 1· true  2 false 3 ask
     * @return
     */
    public int personandcertificateverification(Integer type){
        if(type == 1){
            return 0;
        }else if(type == 2){
            return -1;
        }else if(type == 3){
            return 1;
        }
        return 0;
    }



}
